package org.khd.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.khd.base.annotation.BasePermission;
import org.khd.org.domain.Employee;
import org.khd.org.query.EmployeeQuery;
import org.khd.org.service.IEmployeeService;
import org.khd.base.utils.AjaxResult;
import org.khd.base.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description = "员工相关的CRUD功能")
@BasePermission(name = "员工管理")
public class EmployeeController {

    @Autowired
    private IEmployeeService iEmployeeService;

    //新增和修改Employee数据
//    @RequestMapping("/insertOrUpdateEmp")
    @PutMapping
    @BasePermission(name = "员工的添加或修改")
    public AjaxResult insertOrUpdateEmp(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                iEmployeeService.insertEmp(employee);
            }else {
                iEmployeeService.updateEmp(employee);
            }
            return AjaxResult.ARDefault();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ARDefault().setSuccess(false).setMsg("操作失败" + e.getMessage());
        }
    }

    //通过id删除Employee数据
//    @RequestMapping("/deleteEmpById")
    @DeleteMapping("/{id}")
    @BasePermission(name = "删除单个员工")
    public AjaxResult deleteEmpById(@PathVariable("id") Long id){
        try {
            iEmployeeService.deleteEmpById(id);
            return AjaxResult.ARDefault();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ARDefault().setSuccess(false).setMsg("操作失败" + e.getMessage());
        }
    }

    //通过id查询Employee数据
//    @RequestMapping("/queryEmpById")
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询Employee")
    @BasePermission(name = "查询单个员工")
    public Employee queryEmpById(@PathVariable("id") @ApiParam(value = "员工id") Long id){
        try {
            return iEmployeeService.queryEmpById(id);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //查询Employee数据
//    @RequestMapping("/queryEmpAll")
    @GetMapping
    @BasePermission(name = "查询所有员工")
    public List<Employee> queryEmpAll(){
        try {
            return iEmployeeService.queryEmpAll();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //查询Employee每页数据
    @PostMapping
    @BasePermission(name = "员工分页查询")
    public PageList<Employee> queryEmpPage(@RequestBody EmployeeQuery query){
        try {
            return iEmployeeService.queryEmpPage(query);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //批量删除Employee数据
    @PatchMapping
    @BasePermission(name = "批量删除员工")
    public AjaxResult patchDeleteEmp(@RequestBody List<Long> ids){
        try {
            iEmployeeService.patchDeleteEmp(ids);
            return AjaxResult.ARDefault();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ARDefault().setSuccess(false).setMsg("操作失败" + e.getMessage());
        }
    }

}
